﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace GDLIntegration
{
    public static class GolemBehaviorDefinition
    {
        public static GolemBehavior Behavior(Dictionary<string, GolemAction> actions)
        {
            return new GolemBehavior(){Actions = actions};
        }

        public static GolemAction Action(string evt, ActionType at, string[] parameters )
        {
            return new GolemAction(evt, at, parameters);
        }
        
        public static GolemBehavior ParseGDL(string gdlText)
        {
            /* Tokens */

            //Single line comment
            var singleLineComment = new Regex(@"//.*\n");
            //TODO: remove comments on parse
            //TODO: Remove all newlines

            //Event
            //Events need to be uppercase letters with underscores and preceeded by "handle"
            //i.e. "handle SELF_HP_CRITICAL"
            //event: event name
            //inner: code in the event block
            var eventRegex = new Regex(@"handle\s(?<event>\b[A-Z_]*\b)\[(?<inner>.*)\]");
            
            //Statement
            //matches syntax like "move left 10;" or "attack;" or "castspell 3;"
            var actionStatement = new Regex(@"\b[a-z]{3,}\b\s*\b[a-z0-9]*\b\s*\b[0-9]*\b;");

            //If-else statement
            //condition: the inner expression which can be evaluated to a bool
            //ifblock: statements in the if block
            //elseblock: statements in the else block
            //TODO: else if() support
            var ifElseExpression = new Regex(@"if\((?<condition>.*)\){(?<ifblock>[a-z0-9\s;\(\)\{\}]*)}"
                                             + @"else{(?<elseblock>[a-z0-9\s;\(\)\{\}]*)}");
            
            //variable declarations and assignments
            //name: variable name
            //value: literal or statement defining the value
            #warning Make sure stats are read only!!
            var variableDeclaration = new Regex(@"var\s");
            var variableAssignment = new Regex(@"(?<name>\w*)\s*=\s*(?<value>.*);");
            var variableInitialization = new Regex(@"var\s(?<name>\w*)\s*=\s*(?<value>.*)");

            //evaluates expression for read only stats property reference like "stats->health"
            //name: name of the referenced property
            var propertyReference = new Regex(@"stats->(?<name>\w*)");
            


            return new GolemBehavior();
        }
    }
}
